package com.qyi.activemq.queue.producer;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author luqi
 * @Date 2021/3/11 10:07
 * @Description TODO
 */
@Service
@Slf4j
public class MqProducerService {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 事务-配合JmsTransactionManager使用
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void sendTrans(String msg) {
        String finalMsg = msg + DateUtil.now();
        for (int i = 0; i < 5; i++) {
            log.info("producer Transactional:{}-{}", finalMsg, i);
            jmsMessagingTemplate.convertAndSend(new ActiveMQQueue("q4-trans"), finalMsg);
            if (i == 3) {
                throw new RuntimeException();
            }
        }
    }

}
